<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip"
                       :style="{color}">{{tips}}</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    // api
    import {login} from 'api/login'

    // utils
    import axios from 'axios'
    import 'common/js/md5'

    const loading = '正在登录...'
    const success = '登录成功，正在跳转...'
    const error = '用户名或密码错误'

    export default {
        created() {},
        data () {
            return {
                tips: '',
                form: {
                    userName: '',
                    password: ''
                },
                rules: {
                    userName: [
                        { required: true, message: '账号不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            handleSubmit () {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        let username = this.form.userName,
                            password = this.form.password
                        this.login({
                            username,
                            password
                        })
                    }
                });
            },
            login({username='', password=''}) {
                this.tips = loading
                login({
                    name: username,
                    password: $.md5(password)
                }).then(res => {
                    if (res === 'success') {
                        this.tips = success
                        this._loginSuccess()
                    } else {
                        this.tips = error
                    }
                }).catch(e => {
                    throw new Error(e)
                })
            },
            _loginSuccess() {
                this.$router.push({
                    name: 'uploader'
                });
            }
        },
        computed: {
            color() {
                if (this.tips === loading) {
                    return '#c3c3c3'
                } else if (this.tips === success) {
                    return '#00c100'
                } else if (this.tips === error) {
                    return 'red'
                }
            }
        }
    };
</script>

<style lang="scss" scoped>
    @import "~common/sass/variables";
    @import "~common/sass/mixin";

    .login{
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url('https://file.iviewui.com/iview-admin/login_bg.jpg');
        background-size: cover;
        background-position: center;
        & .login-con{
            position: absolute;
            right: 160px;
            top: 50%;
            transform: translateY(-60%);
            width: 300px;
            .form-con{
                padding: 10px 0 0;
            }
            .login-tip{
                font-size: 10px;
                text-align: center;
                color: #c3c3c3;
            }
        }
    }
</style>